package com.zc.campus_shops.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zc.campus_shops.dao.ShopDao;
import com.zc.campus_shops.dto.ImageHolder;
import com.zc.campus_shops.dto.ShopExecution;
import com.zc.campus_shops.entity.Shop;
import com.zc.campus_shops.enums.ShopStateEnum;
import com.zc.campus_shops.exception.ShopOperationException;
import com.zc.campus_shops.service.ShopService;
import com.zc.campus_shops.util.ImageUtil;
import com.zc.campus_shops.util.PageCalculator;
import com.zc.campus_shops.util.PathUtil;

@Service
public class ShopServiceImpl implements ShopService {

	@Autowired
	private ShopDao shopDao;

	
	@Override
	@Transactional
	public ShopExecution addShop(Shop shop,ImageHolder thumbnail) {
		// TODO Auto-generated method stub
		// 空值判断
		if (shop == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}
		try {
			// 给店铺信息赋初始值
			shop.setEnableStatus(0);
			shop.setCreateTime(new Date());
			shop.setLastEditTime(new Date());
			shop.setAdvice("审核中");
			// 添加店铺信息
			int effectedNum = shopDao.insertShop(shop);
			if (effectedNum <= 0) {
				// 使用RuntimeException可以使事务停止回滚
				throw new ShopOperationException("店铺创建失败");
			} else {
				if (thumbnail.getImage() != null) {
					// 存储图片
					try {
						System.out.println("shopImg234" + thumbnail.getImage());
						addShopImg(shop, thumbnail);
					} catch (Exception e) {
						throw new ShopOperationException("addShopImg error:" + e.getMessage());
					}
				}
				// 更新店铺的图片地址
				effectedNum = shopDao.updateShop(shop);
				if (effectedNum <= 0) {
					throw new ShopOperationException("更新图片失败");
				}
			}
		} catch (Exception e) {
			throw new ShopOperationException("addShop error:" + e.getMessage());
		}
		return new ShopExecution(ShopStateEnum.CHECK, shop);
	}

	private void addShopImg(Shop shop,ImageHolder thumbnail) {
		// TODO Auto-generated method stub
		// 获取shop图片目录的相对值路径
		String dest = PathUtil.getShopImagePath(shop.getShopId());
		System.out.println("dest:" + dest);
		String shopImgAddr = ImageUtil.generateThumbnail(thumbnail, dest);
		System.out.println("shopImgAddr:" + shopImgAddr);
		shop.setShopImg(shopImgAddr);

	}

	// 通过id获取店铺
	@Override
	public Shop getByShopId(long shopId) {
		// TODO Auto-generated method stub
		return shopDao.queryByShopId(shopId);
	}

	@Override
	public ShopExecution modifyShop(Shop shop,ImageHolder thumbnail)
			throws ShopOperationException {
		// 判断店铺是否存在或者是否为空
		if (shop == null || shop.getShopId() == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		} else {
			// 1.判断是否需要处理图片(修改图片之类的操作)
			try{
			if (thumbnail.getImage() != null && thumbnail.getImageName() != null && !"".equals(thumbnail.getImageName())) {
				Shop tempShop = shopDao.queryByShopId(shop.getShopId());
				if (tempShop.getShopImg() != null) {
					ImageUtil.deleteFileOrPath(tempShop.getShopImg());
				}
			}

			addShopImg(shop,thumbnail);

			// 2.更新店铺信息
			shop.setLastEditTime(new Date());
			int effectedNum = shopDao.updateShop(shop);
			if(effectedNum <= 0){
				return new ShopExecution(ShopStateEnum.INNER_ERROR);
			}else{
				shop = shopDao.queryByShopId(shop.getShopId());
				return new ShopExecution(ShopStateEnum.SUCCESS,shop);
			}}catch(Exception e){
				throw new ShopOperationException("modifyShop error:" +e.getMessage());
			}

		}

	}

	/**
	 * 获取商铺列表并分页查询
	 */
	@Override
	public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		List<Shop> shopList = shopDao.queryShopList(shopCondition, rowIndex, pageSize);
		int count = shopDao.queryShopCount(shopCondition);
		ShopExecution se = new ShopExecution();
		if(shopList != null){
			se.setShopList(shopList);
			se.setCount(count);
		}else{
			se.setState(ShopStateEnum.INNER_ERROR.getState());
		}
		return se;
	}

}
